<?php
defined ( 'BASEPATH' ) or die ( 'forbidden' );
class newnoticemanager_Controller extends Controller {
    private $type;
    private $languageType;
    private $begintime;
    private $endtime;
    private $tmpsequence;
    private $areaList;    //区号列表
    private $wholeServerHost;    //全局地址

    function _init() {
        $this->type = array (
                '1' => translate ( 'TYPE1' ),
                '2' => translate ( 'TYPE2' ) 
        );

        $this->languageType = array(
            '1' => translate('LANGUAGE1'),
            '2' => translate('LANGUAGE2'),
            '3' => translate('LANGUAGE3')
        );

        $sql = "select COUNT(id) AS num from tt_notice";
        $one = $this->db->one($sql);
        for ($i = 1; $i <= $one['num']; $i++)
        {
            $this->tmpsequence[$i] = $i;
        }

        $config = get_config();
        $this->wholeServerHost = $config['host_url'];
        $server_list=get_server_list();
        foreach($server_list as $k=>$row) {
            $this->areaList[$row['SvrOutAddr']] = $row['SvrID']."-".$row['SvrName'][1];
        }
    }
    function index() {
        $this->view->display ( "notice/newnoticemanager/index" );
    }
    function flexigrid() {
        list ( $colkey, $colsinfo, $where, $sortname, $sortorder, $offset, $rp, $page ) = $this->common->get_flexigrid_params ();
        $sql = "SELECT COUNT(id) AS num FROM tt_notice WHERE {$where}";
        $one = $this->db->one ( $sql );
        $total = $one ['num'];
        $orderby = $sortname ? "ORDER BY {$sortname} {$sortorder} " : "";
        $sql = "SELECT {$colsinfo} FROM tt_notice WHERE {$where}";
        $this->db->query ( $sql );
        while ( $n = $this->db->get_array () ) {
            $n ['type'] = $this->type [$n ['type']];
            $row [] = array (
                    'id' => $n ['id'],
                    'cell' => $n 
            );
        }
        $result = array (
                'page' => $page,
                'total' => $total,
                'rows' => $row 
        );
        echo json_encode ( $result );
    }
    function add() {
        if (is_submit ()) {
            $rowdata = $_POST ['row'];
            //var_dump($rowdata);
            $game_host=$rowdata['area'];
            $rowdata['operator'] = $this->auth->fields['username'];
            $rowdata['inserttime'] = date('Y-m-d H:i:s', time());
            $rowdata['area']=json_encode($rowdata['area']);
            //$rowdata['tmpsequence'] = 1; //新添加的公告默认最前面显示
            
            $result = $this->db->insert ( 'tt_notice', $rowdata );
            if ($result) {
                   cp_tips ("success");
            }else{
                $result = translate ( 'C_TIPS_ERROR_TEXT' );
                error ( $result );
            }

            //$post_data['SessionID']="ewewqw";
            //$post_data['SessionKey']="wqqew";
            //$post_data['Type']=(int)$rowdata['type']; // 登陆公告：1 ,   运维公告 2
            //$dt = new DateTime($rowdata['starttime']);
            //$post_data['StartTime'] = $dt->getTimestamp();
            //$post_data['Interval']=(int)$rowdata['timesec'];
            //$post_data['Count']=(int)$rowdata['tmpsequence'];
            //$post_data['Notice']=(string)$rowdata['content'];
            //$post_data['NoticeID']=(int)$rowdata['id'];

            //var_dump($post_data);

            //if ($post_data['Type'] == 1){ // 登陆公告
            //    $post_url = $this->wholeServerHost.'gm_set_public_notice';
            //    echo "设置登陆公告的地址:$post_url";

            //    $pnResult = vpost($post_url,3,json_encode($post_data));
            //    if ($result and $pnResult ) {
            //        cp_tips ("success");
            //    } else {
            //        $result = translate ( 'C_TIPS_ERROR_TEXT' );
            //        error ( $result );
            //    }
            //}else{ // 运营公告
            //    foreach ($game_host as $key=>$value){
            //        $postResult = vpost($value.'/gm_Send_Notice',3,json_encode($post_data));
            //        if ($result and $postResult ) {
            //            cp_tips ("success");
            //        } else {
            //            $result = translate ( 'C_TIPS_ERROR_TEXT' );
            //            error ( $result );
            //        }
            //    }
            //}
            //$postResult = vpost($_SESSION['area_selected_host'].'/gm_Send_Notice',3,json_encode($post_data));
        }

        $this->view->assign ( "type", $this->form->select ( "row[type]", $this->type, '', array ('id' => 'type') ) );
        $this->view->assign("languagetype", $this->form->select("row[languagetype]", $this->languageType, '', array('id' => 'languagetype')));
        $this->view->assign("arealist", $this->areaList);
        $this->view->assign('starttime', date('Y-m-d H:i:s', time()));
        $this->view->display ( "notice/newnoticemanager/add" );
    }

    function edit($id = 0) {
        $arr = array();
        $sql = "select type,name,languagetype,timesec,title,content,tmpsequence,starttime from tt_notice where id=$id";
        $row = $this->db->one($sql);
        unset($arr);
        foreach ($row as $key => $value) {
            $arr[$key] = $value;
        }

        if (is_submit()) {
            $rowdata = $_POST['row'];
            
            $rowdata['operator'] = $this->auth->fields['username'];
            $rowdata['inserttime'] = date('Y-m-d H-i-s', time());

            //$game_host=$rowdata['area'];
            //$rowdata['area']=json_encode($rowdata['area']);

            $result = $this->db->update('tt_notice', $rowdata, array('id' => $id));
            if ($result) {
                $arr2 = array();
                $sql = "select type,name,languagetype,timesec,title,content,tmpsequence,starttime from tt_notice where id=$id";
                $row = $this->db->one($sql);
                unset($arr2);
                foreach ($row as $key => $value) {
                    $arr2[$key] = $value;
                }

                $one = $this->db->one("select type,name,languagetype,timesec,title,content from tt_notice where id=$id");
                $one['notice_type'] = $rowdata['type'];
                $one['notice_id'] = $rowdata['id'];
                $one['content'] = $rowdata['content'];
                insert_log(translate('MENU_NOTICE_NEWMANAGER'),translate('EDIT'), $one);
                //success(translate('C_TIPS_SUCCESS_TEXT'));
                $config = get_config();

                $post_data['SessionID']="ewewqw";
                $post_data['SessionKey']="wqqew";
                $post_data['Type']=(int)$rowdata['type']; // 登陆公告：1 ,   运维公告 2
                $post_data['LanguageType']=(int)$rowdata['languagetype']; // 登陆公告：1 ,   运维公告 2

                //$dt = new DateTime($rowdata['starttime']);
                date_default_timezone_set("PRC");
                $dt = new DateTime($arr2['starttime']);
                $post_data['StartTime'] = $dt->getTimestamp();

                $post_data['Interval']=(int)$rowdata['timesec'];
                $post_data['Count']=(int)$rowdata['tmpsequence'];
                $post_data['Notice']=(string)$rowdata['content'];
                $post_data['NoticeID']=(int)$rowdata['id'];

                if ($post_data['Type'] == 1){ // 登陆公告
                    //$post_url = $this->wholeServerHost.'gm_set_public_notice';
                    $post_url = $config['host_url_acc'] . "gm_set_public_notice";
					echo "设置登陆公告的地址:$post_url";

                    $pnResult = vpost($post_url,3,json_encode($post_data));
                    if ($result and $pnResult ) {
                        cp_tips ("success");
                    } else {
                        $result = translate ( 'C_TIPS_ERROR_TEXT' );
                        error ( $result );
                    }
                }else{ // 运营公告
                    $game_host=$rowdata['area'];
                    foreach ($game_host as $key=>$value){					
                        $postResult = vpost("https://".$value.'/gm_Send_Notice',3,json_encode($post_data));
                        if ($result and $postResult ) {
                            //cp_tips ("success");
                        } else {
                            $result = translate ( 'C_TIPS_ERROR_TEXT' );
                            error ( $result );
                        }
                    }
                }
                cp_tips ("success");
            } else {
                $result = translate('C_TIPS_ERROR_TEXT');
                error($result);
            }            
        }

        $this->view->assign("arealist", $this->areaList);
        //$this->view->assign('type', $this->form->select('row[type]', $this->type, array('type' => $arr['type']), array('id' => 'type', 'disabled' => 'disabled')));
        $this->view->assign('type', $this->form->select('row[type]', $this->type, array('type' => $arr['type']), array('id' => 'type')));
        $this->view->assign('languagetype', $this->form->select('row[languagetype]', $this->languageType, array('languagetype' => $arr['languagetype']), array('id' => 'languagetype')));

        //$this->view->assign ("type", $this->form->select ( "row[type]", $this->type, '', array ('id' => 'type') ) );
        $this->view->assign('starttime', $arr['starttime']);
        $this->view->assign('timesec', $arr['timesec']);
        $this->view->assign('tmpsequence', $arr['tmpsequence']);
        $this->view->assign('content', $arr['content']);
        $this->view->assign('id', $id);

        $this->view->display("notice/newnoticemanager/edit");
    }

    function del($id = 0) {
        $rowdata = $_POST['row'];

        $c = 0;
        $r = translate('C_TIPS_SUCCESS_TEXT');
        $one = $this->db->one("select type,name,languagetype,content,tmpsequence,area from tt_notice where id=$id");
        $game_host=json_decode($one['area']);

        $post_data['SessionID']="ewewqw";
        $post_data['SessionKey']="wqqew";
        $post_data['NoticeID']=(int)$id;

        $where = array('id' => $id);

        //insert_log(translate('MENU_NOTICE_NEWMANAGER'),translate('DEL'), array('id'=>$id));
        //$config = get_config();

        if ($one['type'] == 1){ // 登陆公告
             $post_url = $this->wholeServerHost.'gm_del_public_notice';
             $pnResult = vpost($post_url,3,json_encode($post_data));
             if ( !$pnResult ) {
                 $r = translate('C_TIPS_ERROR_TEXT');
                 error ( $r );
             }
        }else{ // 运营公告
             foreach ($game_host as $key=>$value){
                 $postResult = vpost($value.'/gm_Del_Notice',3,json_encode($post_data));
                 if ( !$postResult ) {
                     $r = translate('C_TIPS_ERROR_TEXT');
                     error ( $r );
                 }
             }
        }

        //json_output($c, $r);
        $this->db->delete('tt_notice', $where);

        cp_tips ("success");
    }
    
    function multi() {
        $type = $this->input->post("type");
        $ids = $this->input->post("ids");
        $c = 0;
        $r = '';
        if (count(explode(',', $ids)) == 0) {
            $r = translate('C_INVALID_PARAMS');
        } else {
            if ($type == 'delete') {
                $this->db->query("DELETE FROM tt_notice WHERE id IN ({$ids})");
                insert_log(translate('MENU_NOTICE_NEWMANAGER'),translate('MULTI_DEL'),array('ids'=> $ids));
                $c = 1;
                $config = get_config();
                $URL_NOTICE = $config['host_url_backstage'] . "master/duoluotaitanGM/api/sendNoticeInfo";
                #var_dump($URL_NOTICE);
                #exit(0);
                $value = file_get_contents($URL_NOTICE);
            } else {
                $r = translate('C_INVALID_PARAMS');
            }
        }
        json_output($c, $r);
    }
}







?>
